import React from 'react';
import { Modal, Form, Input, message } from 'antd';
import {assignTask} from "@/services/ant-design-pro/api";

interface AssignTesterModalProps {
  taskId: number;
  modalVisible: boolean;
  onCancel: () => void;
  onAssignSuccess: () => void;
}

const AssignTesterModal: React.FC<AssignTesterModalProps> = ({
                                                               taskId,
                                                               modalVisible,
                                                               onCancel,
                                                               onAssignSuccess,
                                                             }) => {
  const [form] = Form.useForm();

  const handleAssignTester = async () => {
    try {
      const values = await form.validateFields();
      const tester = values.tester;

      const result = await assignTask(taskId, tester);

      if (result.code === 200) {
        message.success('任务指派成功');
        onAssignSuccess?.();
        onCancel();
      } else {
        message.error(result.description || '任务指派失败，请重试');
      }
    } catch (error) {
      message.error('任务指派失败，请检查输入并重试');
    }
  };

  return (
    <Modal
      title="任务指派"
      visible={modalVisible}
      onCancel={onCancel}
      onOk={handleAssignTester}
    >
      <Form form={form}>
        <Form.Item
          name="tester"
          label="测试人员"
          rules={[{ required: true, message: '请输入测试人员名称' }]}
        >
          <Input placeholder="请输入测试人员名称" />
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default AssignTesterModal;
